package com.android.swipe;

import android.graphics.Color;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import java.util.Random;

public class MainActivity extends AppCompatActivity {

    private ListView lv;
    private SwipeRequestLayout srl;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lv = findViewById(R.id.lv);
        srl = findViewById(R.id.srl);
        lv.setAdapter(new ItemAdapter());

        srl.setRefreshing(true);

        srl.setOnSwipeRefreshListener(new SwipeRequestLayout.OnSwipeRefreshListener() {
            @Override
            public void onSwipeRefresh() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        srl.setRefreshing(false);
                    }
                },500);
            }
        });


        srl.setOnSwipeLoadListener(new SwipeRequestLayout.OnSwipeLoadListener() {
            @Override
            public void onSwipeLoad() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        srl.setLoading(false);
                    }
                },500);
            }
        });
    }

    private class ItemAdapter extends BaseAdapter{
        @Override
        public int getCount() {
            return 50;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            TextView textView = new TextView(MainActivity.this);
            textView.setHeight(120);
            Random random = new Random();
            int r = random.nextInt(256);
            int g = random.nextInt(256);
            int b = random.nextInt(256);
            textView.setBackgroundColor(Color.rgb(r,g,b));
            return textView;
        }
    }

}
